<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="950" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm"
				 label-width="110px"
				 label-position="left">
			<el-form-item label="企业客户" prop="company_id">
				<el-select v-model="form.company_id" filterable style="width: 100%" clearable>
					<el-option v-for="item in companyList" :key="item.company_id" :label="item.company_name" :value="item.company_id"/>
				</el-select>
			</el-form-item>
<!--			<el-form-item label="选择商品" prop="product_id">-->
<!--				<el-select v-model="form.product_id" filterable style="width: 100%" clearable>-->
<!--					<el-option v-for="item in cardList" :key="item.product_id" :label="item.product_name" :value="item.product_id"/>-->
<!--				</el-select>-->
<!--			</el-form-item>-->
			<el-form-item label="选择服务卡" prop="card_id">
				<el-select v-model="form.card_id" filterable style="width: 100%" clearable>
					<el-option v-for="item in cardList" :key="item.card_id" :label="item.card_name" :value="item.card_id"/>
				</el-select>
			</el-form-item>
			<el-form-item label="采购数量" prop="purchase_qty">
				<el-input v-model="form.purchase_qty" placeholder="采购数量" clearable></el-input>
			</el-form-item>
			<el-form-item label="有效期" prop="valid_date">
				<el-date-picker
					v-model="form.valid_date"
					type="datetimerange"
					start-placeholder="请选择开始时间"
					end-placeholder="请选择结束时间"
					format="YYYY-MM-DD HH:mm:ss"
					date-format="YYYY/MM/DD ddd"
					time-format="A hh:mm:ss"
				/>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新建订单',
				edit: '编辑订单',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				company_id: "",
				card_id: "",
				purchase_qty: "",
				valid_date: "",
				remark: "",
			},
			//验证规则
			rules: {
				company_id: [
					{required: true, message: '请选择企业客户', trigger: 'change'}
				],
				card_id: [
					{required: true, message: '请选择商品', trigger: 'change'}
				],
				purchase_qty: [
					{required: true, message: '请输入正确的采购数量，数量必须是大于0小于1000的整数'},
				],
				valid_date: [
					{required: true, message: '请选择有效期'}
				],
			},
			companyList: [],
			cardList: [],
		}
	},
	mounted() {
		this.getCompanyList()
		this.getCardList()
	},
	methods: {
		getCompanyList() {
			this.$API['company.company'].getCompanyList.get().then((res) => {
				if (res.code == 0) {
					this.companyList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		getCardList() {
			this.$API['service_card.service_card'].cardSelect.get().then((res) => {
				if (res.code == 0) {
					this.cardList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.$API['company.company_order'].store.post(this.form)
						.then((res) => {
							if (res.code == 0) {
								this.$emit('success', this.form, this.mode)
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {
							this.isSaveing = false;
						});
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data = []) {
			this.form.company_id = data.company_id
			this.form.card_id = data.card_id
			this.form.purchase_qty = data.purchase_qty
			this.form.remark = data.remark
			this.form.valid_date = data.valid_date
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
